The present invention generally relates to the Component Object Model (COM), and more particularly to the conversion of COM interfaces to underlying C++ objects.
The Component Object Model (COM) is a software architecture that supports reuse of software components between different application programs written in different languages. Reusing software components, where appropriate, reduces the cost of creating a new application as compared to creating an application from scratch. COM addresses the issues of interoperability where reuse is desirable for binary executables that are created by different entities in different languages.
In COM, a xe2x80x9ccomponentxe2x80x9d is a piece of compiled code that provides one or more services. It will be appreciated that even though a component is also an object in object oriented programming (OOP) terms, the term xe2x80x9ccomponentxe2x80x9d will be used herein to avoid confusion with other objects in general. An xe2x80x9cinterfacexe2x80x9d is the mechanism through which components interact and through which software in general accesses the services of a component. The interface defines the behavior and responsibilities of the component. In other words, the interface is a group of related functions and forms the binary standard or contract through which the component is accessed.
A COM interface is not a class and cannot be instantiated by itself. A COM component must implement the interface, and the component must be instantiated for the interface to exist. It will be appreciated that the same interface can be implemented in different ways by different components with the restriction that each component must satisfy the interface""s behavior and responsibilities.
In some applications there is an active relationship between COM objects. For example, a first COM interface method may take a reference to another COM object as input. This reference is used to identify the COM object against which direct action will take place. In addition, a method of a first interface of a first COM object may be accessed in an interface method of a second COM object. It will be appreciated, however, that there may be certain classes of clients for which access to the method of the first interface is undesirable.
One solution to protect attributes of the COM object (for example, the method of the first interface) is to implement a third COM interface that includes the desired attributes. This third COM interface wouldn""t be documented to normal clients and would only be used internally. This solution, however, suffers from the fact that industrious COM clients could get access to the third COM interface. The rules of COM dictate that an object must indicate, on demand, whether or not it supports a given interface. An object cannot discriminate between types of clients over time when deciding what interfaces it supports. Thus, the third COM interface could be discovered and used by client for which its use was unintended.
A method that addresses the aforementioned problems, as well as other related problems, is therefore desirable.
In various embodiments, the invention provides methods and apparatus for converting from a COM interface pointer to an underlying C++ object. In the various embodiments, classes are constructed in support of the underlying C++ object. The classes are used to enforce rules that allow one to safely convert a COM interface pointer. One rule is that a COM interface to be converted cannot be marshaled. A second rule is that an object requesting a C++ object must have legal access to the COM interface (for example, the same execution unit). A third rule is that the object type of the COM interface must be in the inheritance hierarchy of the C++ object.
In one embodiment, a COM object is instantiated having a first interface referenced with a first interface pointer and a raw interface. The COM object is implemented as a first object of a C++ class that has a public conversion method. The conversion method, responsive to an input of the first interface pointer, returns a resultant pointer to the first object if the COM object supports the raw interface, the raw interface has not been marshaled, and the execution unit that implements the COM object referenced by the first interface pointer is the same execution unit performing the conversion method.
In another embodiment, a COM object is instantiated having a first interface pointer. The COM object is implemented as a first object of a first C++ class, which has a public conversion method and a private registry attribute. The private registry attribute maps an interface pointer of the COM object to an associated pointer to the first object. Responsive to an interface pointer of the COM object that is input to the conversion method, the associated pointer to the first object is returned.
The above summary of the present invention is not intended to describe each disclosed embodiment of the present invention. The figures and detailed description that follow provide additional example embodiments and aspects of the present invention.